#!/bin/bash
script_folder="/home/aqdms/CARBLog/sys_resources/scripts"

while read line; do
    eval $line
done < Config/config

#  $1 = instrument name
#  $2 = month
#  $3 = year
#  $4 = e-mail address
#  $5 = data script
#get list of all data files for the month-year in question
file_list=$(ls Data/$saroad-$3-[0-9][0-9]-$2)

#parse data files for single instrument	
for i in $file_list
do
	cat "$i" | grep "$1" >> $script_folder/$1-$2-$3
done	

if [ "$5" != "" ]; then

	data_folder="/home/aqdms/Desktop/DataSummary"
	if [ ! -d "$data_folder" ]; then 
		#if the data folder does not exist then create it
		mkdir "$data_folder"
	else
		#wipe out the old file
		rm -f "$datafolder/*_$5"
	fi

	command="bash $script_folder/$5 $1 $1-$2-$3"
	eval $command
	wait $!
	echo "Script $5 applied to data"
	cp $script_folder/$1-$2-$3 $data_folder/$1-$2-$3_$5

fi

bzip2 $script_folder/$1-$2-$3
rm -f $script_folder/$1-$2-$3 

# sends the email with the data file as an attachment
command="echo \"Raw data summary for $1 during $2-$3\" | mutt -s \"Raw data summary for
$1 during $2-$3\" -a $script_folder/$1-$2-$3.bz2 -- $4"

eval $command

rm -f $script_folder/$1-$2-$3.bz2

exit


